package cn.cjx.user.service.impl;

import cn.cjx.basic.constant.BaseConstant;
import cn.cjx.basic.dto.RegisterDto;
import cn.cjx.basic.exception.BusinessException;
import cn.cjx.basic.utils.MD5Utils;
import cn.cjx.basic.utils.StrUtils;
import cn.cjx.user.domain.Logininfo;
import cn.cjx.user.domain.User;
import cn.cjx.user.mapper.LogininfoMapper;
import cn.cjx.user.mapper.UserMapper;
import cn.cjx.user.service.IUserService;
import cn.cjx.basic.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cjx
 * @since 2022-05-08
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private LogininfoMapper logininfoMapper;

	@Autowired
	private RedisTemplate redisTemplate;

	@Override
	public void registerPhone(RegisterDto dto) {

		String smsCodeValue = dto.getSmsCodeValue();
		String confirmPassword = dto.getConfirmPassword();
		String password = dto.getPassword();
		//1.空校验
		if (StringUtils.isEmpty(smsCodeValue)) {
			throw new BusinessException("短信验证码为空，请输入短信验证码");
		}
		if (StringUtils.isEmpty(password)) {
			throw new BusinessException("初识密码为空，请输入密码");
		}
		if (StringUtils.isEmpty(confirmPassword)) {
			throw new BusinessException("确认密码为空，请输入密码");
		}

		//校验两次密码是否一致
		if (!password.equals(confirmPassword)) {
			throw new BusinessException("两次输入的密码不一致，请仔细检查密码");
		}

		//校验短信验证码
		String smsCode = BaseConstant.VerifyCodeConstant.BUSINESS_REGISTER_PREFIX + dto.getPhone();//验证码与手机号拼接
		Object smsCodeTemp = redisTemplate.opsForValue().get(smsCode);
		if (smsCodeTemp == null) {
			throw new BusinessException("验证码无效或已过期，请重新获取");
		}
		String smsCodeVal = smsCodeTemp.toString().split(":")[0];
		if (!smsCodeVal.equalsIgnoreCase(smsCodeValue)) {
			throw new BusinessException("验证码错误，请输入正确的验证码");
		}
			//throw new BusinessException("密码正确，登陆成功");

		//保存数据
		/*
		注意：
		1.当两个表的相同字段很多时，一般我们先创建字段多的表,
		然后我们创建另一张表时可以使用Spring提供的工具类BeanUtils
		2.当我们保存保存表时，如果一张表中有另一张表的外键，
		此时我们可以先创建另一张表，然后根据外键来保存
		*/
		//保存数据
		User user = dto2User(dto);
		Logininfo logininfo = user2Logininfo(user);
		//先保存t_logininfo，然后返回自增id
		logininfoMapper.save(logininfo);
		//根据logininfoId来保存user
		user.setLogininfo_id(logininfo.getId());
		userMapper.save(user);
	}

	private Logininfo user2Logininfo(User user) {
		Logininfo logininfo = new Logininfo() ;
		BeanUtils.copyProperties(user,logininfo);
		logininfo.setType(1);
		return logininfo;
	}

	private User dto2User(RegisterDto dto) {
		User user = new User();
		user.setUsername(dto.getPhone());
		user.setPhone(dto.getPhone());

		String salt = StrUtils.getComplexRandomString(32);
		String md5Pwd = MD5Utils.encrypByMd5(dto.getPassword() + "cjx" + salt + "xxxx");

		user.setSalt(salt);
		user.setPassword(md5Pwd);
		return user;
	}

	@Override
	public void update(User user) {
		super.update(user);
		//修改user的时候，修改logininfo
	}

	@Override
	public void remove(Long id) {
		super.remove(id);
		//修改user的时候，修改logininfo
	}

	@Override
	public void loadByPhone(String phone) {
		 userMapper.loadByPhone(phone);
	}
}
